package com.zzy.chapter4.transaction.mediator;

import com.zzy.chapter4.transaction.colleague.AbstractCustomer;
import com.zzy.chapter4.transaction.colleague.Buyer;
import com.zzy.chapter4.transaction.colleague.Payer;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class Mediator extends AbstractMediator{

    private AbstractCustomer buyer;

    private AbstractCustomer payer;

    public final ConcurrentHashMap<String, Map<String, AbstractCustomer>> currentCustomerInstance = new ConcurrentHashMap<>();

    @Override
    public void messageTransfer(String orderId, String targetConsumer, AbstractCustomer customer, String payResult) {
        if (customer instanceof Buyer){
            AbstractCustomer buyer = currentCustomerInstance.get(orderId).get("buyer");
            System.out.println("朋友代付：" + buyer.getCustomerName() + " 转发订单" + orderId + "到用户" + targetConsumer + "进行支付.");
        } else if (customer instanceof Payer) {
            AbstractCustomer payer = currentCustomerInstance.get(orderId).get("payer");
            System.out.println("代付完成：" + payer.getCustomerName() + "完成订单" + orderId + "的支付.通知" + targetConsumer + "支付成功.");
            currentCustomerInstance.remove(orderId);
        }
    }
}
